package be.fedict.eid.viewer;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import javax.smartcardio.Card;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;

import be.fedict.commons.eid.client.BeIDCard;
import be.fedict.commons.eid.client.FileType;
import be.fedict.commons.eid.client.spi.UserCancelledException;
import be.fedict.commons.eid.consumer.Address;
import be.fedict.commons.eid.consumer.CardData;
import be.fedict.commons.eid.consumer.Identity;
import be.fedict.commons.eid.consumer.tlv.ByteArrayParser;
import be.fedict.commons.eid.consumer.tlv.TlvParser;

public class CardTerminalManager
{
    private CardTerminal 	terminal;
    private FileType[] 		readOrder;
    private byte[]	 	identityFile;
    private byte[]	 	addressFile;
    private Identity 	 	identity;
    private Address	 	address;
    
    public CardTerminalManager(CardTerminal terminal)
    {
	this(terminal,new FileType[]{
		FileType.Identity,FileType.Address, FileType.Photo, 
		FileType.AuthentificationCertificate, FileType.NonRepudiationCertificate});
    }
    
    public CardTerminalManager(CardTerminal terminal, FileType[] readOrder)
    {
	super();
	this.terminal = terminal;
	this.readOrder = readOrder;
    }
    
    public void eIDCardInserted(BeIDCard card) {
	
	try {
	    
	    card.selectApplet();
	    identityFile=card.readFile(FileType.Identity);
	    identity=TlvParser.parse(identityFile, Identity.class);
	    addressFile=card.readFile(FileType.Address);
	    address=TlvParser.parse(addressFile, Address.class);
	    System.out.println(identity.getFirstName() + " " + identity.getName() + " from " + address.municipality);

	} catch (CardException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} 
		
   	
       }

       public void eIDCardRemoved(BeIDCard card) {
        identity=null;
        address=null;
   	identityFile=null;
   	addressFile=null;
   	
       } 

    public void otherCardInserted(Card card) {
	// TODO Auto-generated method stub
	
    }

    public void otherCardRemoved() {
	// TODO Auto-generated method stub
	
    }

   
}
